const mongoose = require('mongoose');

const answerSchema = new mongoose.Schema({
    content: {
        type: String,
        required: [true, '回答内容是必需的'],
        minlength: [10, '回答内容至少需要10个字符']
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    createdAt: {
        type: Date,
        default: Date.now
    }
});

const qaSchema = new mongoose.Schema({
    question: {
        type: String,
        required: [true, '问题是必需的'],
        trim: true,
        minlength: [5, '问题至少需要5个字符'],
        maxlength: [200, '问题不能超过200个字符']
    },
    content: {
        type: String,
        required: [true, '问题描述是必需的'],
        minlength: [10, '问题描述至少需要10个字符']
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    },
    answers: [answerSchema],
    status: {
        type: String,
        enum: ['open', 'closed'],
        default: 'open'
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

// 更新时自动更新updatedAt字段
qaSchema.pre('save', function(next) {
    this.updatedAt = Date.now();
    next();
});

const QA = mongoose.model('QA', qaSchema);

module.exports = QA; 